class Document < ActiveRecord::Base
  belongs_to :source, :polymorphic => true
  belongs_to :user

  has_many :comments, :dependent => :destroy
  has_and_belongs_to_many :tags

  default_scope order('created_at DESC')

  has_attached_file :image,
                    :styles => {:thumb => "128x128" },
                    :default_url => :image_stub

  validates_attachment_size :image,
                            :less_than => 100.kilobytes
  validates_attachment_content_type :image, :content_type => ['image/jpeg', 'image/png', 'image/gif']

  validates :title, :presence => true,
                    :length => {:maximum => 255}
  validates :body,  :presence => true

  private
    def image_stub
      case self.source_type
        when "Article"     then "unknown.png"
        when "Publication" then "atom.png"
        when "News"        then "news.png"
        when "Conference"  then "conference.png"
        when "Report"      then "report.png"
      end
    end
end

